#include "SCore.h"

int SCore::Run()
{
	if( !CoreInit() ) return 0;

	BOOL bGotMsg;
	MSG msg;
	PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE );
	while( WM_QUIT != msg.message )
	{
		if( m_IsActive )
		{
			bGotMsg = PeekMessage( &msg, NULL, 0, 0, PM_REMOVE );
		}
		else
			bGotMsg = GetMessage( &msg, NULL, 0, 0 );
		if( bGotMsg )
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		else
		{
			if( !CoreRun() ) break;
		}
	}
	return 1;
}

bool SCore::CoreRun()
{
	CoreFrame();
	CoreRender();
	
	return true;
}

bool SCore::CoreInit()
{
	m_Frustum.SetDevice( this->m_pd3dDevice );
	m_Camera.SetDevice( this->m_pd3dDevice );
	m_Font.SetDevice( this->m_pd3dDevice );		
	m_Time.Init();
	m_Camera.InIt();
	m_Debug.Init();
	m_Font.Init();
	m_BuseDebugCamera = false;
	m_Input.SetHwnd( this->m_hWnd );	
	Init();
	return true;	
}

bool SCore::CoreFrame()
{
	m_Camera.Frame();	
	m_Time.Frame();
	Frame();

	return true;
}

bool SCore::Init()
{
	return true;
}

bool SCore::Frame()
{
	return true;
}



bool SCore::PreCoreRender()
{
	m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 );
	
	if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
	{
		return true;
	}
	return false;
}

bool SCore::DebugRender()
{
	TCHAR szBuffer[256];
	memset( szBuffer, 0, sizeof(TCHAR)*256);
	_stprintf_s( szBuffer, _T("FPS = %d"), m_Time.m_iFramePerSec );

	RECT rect = { 0,0, 100, 20 };
	m_Font.m_pFont->DrawText(	NULL, szBuffer, -1, &rect,
								DT_LEFT, 0xffff00ff );

	memset( szBuffer, 0, sizeof(TCHAR)*256);
	_stprintf_s( szBuffer, _T("SPF = %12f"), m_Time.m_fSecPerFrame );

	RECT rect2 = { 0, 30, 100, 50 };
	m_Font.m_pFont->DrawText(	NULL, szBuffer, -1, &rect2,
								DT_RIGHT, 0xffff00ff );

	m_Debug.DrawLine(	this->m_pd3dDevice,
						D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
						D3DXVECTOR3( 1000.0f, 0.0f, 0.0f ),
						0xffff0000 );
	m_Debug.DrawLine(	this->m_pd3dDevice,
						D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
						D3DXVECTOR3( 0.0f, 1000.0f, 0.0f ),
						0xff00ff00 );
	m_Debug.DrawLine(	this->m_pd3dDevice,
						D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
						D3DXVECTOR3( 0.0f, 0.0f, 1000.0f ),
						0xff0000ff );
	return true;

}

bool SCore::PostCoreRender()
{
	DebugRender();
	m_pd3dDevice->EndScene();
	m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
	return true;
}

bool SCore::CoreRender()
{
	if(m_pd3dDevice==NULL) return false;
	PreCoreRender();
	Render();
	PostCoreRender();
	return true;
}



bool SCore::Render()
{
	return true;
}

bool SCore::Release()
{
	return true;
}

SCore::SCore(void)
{
	
}

SCore::~SCore(void)
{
	Release();
}
